package p43_FactoryBean;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 工厂bean，用于创建其他bean
 */
@Component("factoryBean")
public class MyFactoryBean implements FactoryBean<Bean1> {

    @Autowired
    public void setHome(@Value("java.home") String home) {
        System.out.println("FactoryBean的依赖注入..." + home);
    }

    @PostConstruct
    public void init() {
        System.out.println("FactoryBean的初始化...");
    }

    @PreDestroy
    public void destroy() {
        System.out.println("FactoryBean的销毁...");
    }

    /**
     * 生成产品
     * @return
     * @throws Exception
     */
    @Override
    public Bean1 getObject() throws Exception {
        return new Bean1();
    }

    /**
     * 该工厂生成产品的类型
     * @return
     */
    @Override
    public Class<?> getObjectType() {
        return Bean1.class;
    }

    /**
     * 创建出来的产品是否单例
     * @return
     */
    @Override
    public boolean isSingleton() {
        return true;
    }
}
